package sz.szsmk.citizencard.ui.activity;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.chenenyu.router.annotation.Route;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.CollectActivityBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.CollecQueryRes;
import sz.szsmk.citizencard.mvp.contract.ICollectionContact;
import sz.szsmk.citizencard.mvp.presenter.CollectionPresenter;
import sz.szsmk.citizencard.ui.adapter.CollecAdapter;
import sz.szsmk.citizencard.ui.adapter.CollecAdapterNew;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.PubUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/13 17:43
 * description:我的收藏
 */
@Route("collection")
public class CollectionActivity extends IBaseActivity<CollectionPresenter, CollectActivityBinding>
        implements ICollectionContact.ICollectionView, View.OnClickListener  {

    private RecyclerView collectionRv;
    private CollecAdapterNew collecAdapter;
    private int currentPager = 1;

    private String mLatitude;
    private String mLongitude;

    @Override
    protected int attachLayoutRes() {
        return R.layout.collect_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        dataBinding.titleLayout.settingTitle.setText("我的收藏");
        dataBinding.titleLayout.back.setOnClickListener(this);
        dataBinding.searchEt.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_UP) {
                    String keyWord = dataBinding.searchEt.getText().toString();
                    if (TextUtils.isEmpty(keyWord)) {
                        PubUtils.popTipOrWarn(CollectionActivity.this, "您未输入任何内容");
                    } else {
//                        mPresenter.shopSearch(keyWord, "32.040358", "118.728776", "15");
                    }

                    return true;
                }

                return false;
            }
        });

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        dataBinding.collectionRv.setLayoutManager(linearLayoutManager);
        collecAdapter = new CollecAdapterNew();
        dataBinding.collectionRv.setLayoutManager(new LinearLayoutManager(this));
        dataBinding.collectionRv.setAdapter(collecAdapter);

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                currentPager = 1;
                mPresenter.queryCollect(mLatitude,mLongitude);
            }
        });
        dataBinding.swipeRefreshLayout.autoRefresh();

    }

    @Override
    protected void updateViews() {
//        mPresenter.queryUserCollectCity();
    }

    @Override
    public void onResume() {
        super.onResume();
    }


    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.back) {
            finish();
        }
    }

    @Override
    public void queryCollectSucc(List<CollecQueryRes.DataBean> data) {
        if(currentPager==1&&data.size()==0){
            View empty = LayoutInflater.from(this).inflate(R.layout.layout_signin_emptydata,null,false);
            ((TextView)empty.findViewById(R.id.tv_tip)).setText("您还没有收藏数据哦~");
            this.collecAdapter.setEmptyView(empty);
        }
        if (currentPager == 1) {
            this.collecAdapter.replaceData(data);
        } else {
            this.collecAdapter.addData(data);
        }
        boolean hasMore = data.size() == 15;
        if (hasMore) {
            currentPager++;
        } else {
            //完成加载并标记没有更多数据
            dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
        }
    }

    @Override
    public void queryCollectFail() {
    }

    @Override
    public void queryUserCollectCityFail() {
    }

    @Override
    public void queryUserCollectCitySucc() {

    }

}
